/**
 * 浮点精度问题
 */

// 0.1 + 0.2 = 0.30000000000000004
// 0.1 + 0.7 = 0.7999999999999999
// 0.11 + 0.1 = 0.21000000000000002
// 1.1 + 1.3 = 2.4000000000000004

// fix(0.1, 0.2)
// fix(0.1, 0.7)
// fix(0.11, 0.1)
// fix(1.1, 1.3)

function fix(x, y){
    // 判断小数位数
    let x_length = x.toString().split(".")[1].length
    let y_length = y.toString().split(".")[1].length
    // 取最大的小数位数
    let length = x_length > y_length ? x_length : y_length
    // 进位取整，计算完成后再变回小数
    let auxiliary = Math.pow(10, length)
    let result = (x * auxiliary + y * auxiliary) / auxiliary

    console.log(result)
    return result
}

// 不限制传入个数
function add(){
    let sum = 0
    let arr_length = []
    for(let i = 0; i < arguments.length; i++){
        let arg_length = arguments[i].toString().split(".")[1].length
        arr_length.push(arg_length)
    }
    function sortNumber(a, b){
        return b - a
    }
    //从大到小排序
    arr_length.sort(sortNumber)
    let length = arr_length[0]
    let auxiliary = Math.pow(10, length)

    for(let i = 0; i < arguments.length; i++){
        sum = (arguments[i] * auxiliary + sum * auxiliary) / auxiliary
    }
    console.log(sum)
    return sum
}

// add(0.1, 0.2)
// add(0.1, 0.7)
// add(0.11, 0.1)
// add(1.1, 1.3)
add(1.222, 1.2222, 1.43, 0.2)